<template>
  <section class="jumbotron">
    <h3 class="jumbotron-heading">Search Github Users</h3>
    <div>
      <input
        type="text"
        v-model="keyWord"
        placeholder="enter the name you search"
      />
      &nbsp;
      <button @click="getUsers">Search</button>
    </div>
  </section>
</template>

<script>
import axios from 'axios';
export default {
  name: 'Search',
  data() {
    return {
      keyWord: '',
    };
  },
  methods: {
    getUsers() {
      this.$bus.$emit('getUsers', {
        isFirst: false,
        isLoading: true,
        users: [],
        errorMsg: '',
      });
      axios.get(`https://api.github.com/search/users?q=${this.keyWord}`).then(
        response => {
          console.log('请求成功，获取的数据是:', response.data.items);
          this.$bus.$emit('getUsers', {
            isLoading: false,
            users: response.data.items,
            errorMsg: '',
          });
        },
        error => {
          this.$bus.$emit('getUsers', {
            isLoading: false,
            users: [],
            errorMsg: error.message,
          });
        }
      );
    },
  },
};
</script>

<style></style>
